<?php

namespace Apps\Common\Models;

use Phalcon\Mvc\Model\Resultset\Simple as ResultSet;

/**
 * 问题的回答Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class QuestionAnswer extends \PhalconPlus\Database\Model
{

    /**
     *
     * @var integer
     */
    public $answer_id;

    /**
     *
     * @var integer
     */
    public $question_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var integer
     */
    public $answer_floor;

    /**
     *
     * @var string
     */
    public $answer_content;

    /**
     *
     * @var string
     */
    public $answer_pics;

    /**
     *
     * @var integer
     */
    public $answer_follows;

    /**
     *
     * @var integer
     */
    public $answer_state;

    /**
     *
     * @var string
     */
    public $answer_addtime;
    public $city_code;
    public $support_count;
    public $type;
    public $original_content;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->hasMany('answer_id', 'Apps\Common\Models\QuestionAnswerFollow', 'answer_id', array('alias' => 'QuestionAnswerFollow'));
        $this->belongsTo('question_id', 'Apps\Common\Models\Question', 'question_id', array('alias' => 'Question'));
        $this->belongsTo('user_id', 'Apps\Common\Models\UserBase', 'user_id', array('alias' => 'UserBase'));
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'question_answer';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return QuestionAnswer[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return QuestionAnswer
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'answer_id' => 'answer_id',
            'question_id' => 'question_id',
            'user_id' => 'user_id',
            'answer_floor' => 'answer_floor',
            'answer_content' => 'answer_content',
            'answer_pics' => 'answer_pics',
            'answer_follows' => 'answer_follows',
            'answer_state' => 'answer_state',
            'answer_addtime' => 'answer_addtime',
            'city_code' => 'city_code',
            'support_count' => 'support_count',
            'type' => 'type',
            'original_content' => 'original_content',
        );
    }

    public function defaultColumns()
    {
        return [
            'answer_pics',
            'answer_follows',
            'answer_state',
            'answer_addtime',
        ];
    }

    public static function listByQuestionId($questionId, $limit, $offset, $userId)
    {
        $sql = <<<sqlStr
SELECT
	ub.user_id, user_nickname, user_cover, user_expert,
	(SELECT COUNT(*) FROM question_answer_follow WHERE user_id=:uid AND answer_id=qa.answer_id) user_is_follow,
	answer_id, answer_content, answer_pics,
	answer_follows answer_best,
	answer_follows follow_num,
	answer_addtime addtime
FROM question_answer as qa
LEFT JOIN user_base as ub ON qa.user_id = ub.user_id
WHERE answer_state > 0 AND question_id = :qid
ORDER BY user_expert DESC , addtime desc
LIMIT $limit OFFSET $offset;
sqlStr;
        $model = new self;
        return new ResultSet(
            null,
            $model,
            $model->getReadConnection()->query($sql, ['qid' => $questionId, 'uid'=>$userId])
        );
    }

    /**
     * 更新关注数
     * @param $id
     * @return bool
     */
    public static function updateFollows($id)
    {
        $sql = 'UPDATE question_answer SET answer_follows = (SELECT COUNT(*) FROM question_answer_follow WHERE answer_id=question_answer.answer_id) WHERE answer_id = :aid';
        $model = new self;
        return $model->getWriteConnection()->execute($sql, ['aid' => $id]);
    }

    /**
     * 修改回复的点赞数
     * @param $answer_id
     * @param int $count
     */
    public static function setSupportCount($answer_id, $count = 1)
    {
        (new self)->getReadConnection()->query("UPDATE question_answer SET support_count = support_count + {$count} WHERE answer_id = {$answer_id}");
    }
}
